package com.obtk.controllerSW;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.Customer;
import com.obtk.service.CustomerService2;
import com.obtk.util.*;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-03-29 10:30:08
 */
@Controller
@RequestMapping("/customer2")
public class CustomersController2 {
    @Autowired
    private CustomerService2 customerService2;
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(Customer customer, Integer limit, Integer offset,Model model){
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<Customer> customersList = customerService2.selectCustomer(customer);
        PageInfo<Customer> pages = new PageInfo<>(customersList);
        System.out.println(customersList);

        return ResultUtil.table(customersList,pages.getTotal());
    }
    /**新增用户*/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(Customer customerForm, String confirmPassword){
        String customerName = customerForm.getCustName();
        Customer customer = customerService2.selectByCustomerName(customerName);
        if (null != customer) {
            return ResultUtil.error("用户名已存在");
        }
        int num = customerService2.register(customerForm);
        if(num > 0){
            return ResultUtil.success("添加用户成功");
        }else {
            return ResultUtil.error("添加用户失败");
        }
    }


    /**编辑用户详情*/
    @GetMapping("/edit")
    public String customerDetail(Model model, Integer custId){
        Customer customer = customerService2.selectByCustomerId(custId);
        model.addAttribute("customer", customer);
        return "customer2/customerDetail";
    }

    /**编辑用户*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(Customer customerForm){
        int a = customerService2.updateByCustomerId(customerForm);
        if (a > 0) {
            return ResultUtil.success("编辑用户成功！");
        } else {
            return ResultUtil.error("编辑用户失败");
        }
    }
    /**删除用户*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String custId) {
        List<String> customerIdsList = Arrays.asList(custId);
        int a = customerService2.updateStatusBatch(customerIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除用户成功");
        } else {
            return ResultUtil.error("删除用户失败");
        }
    }
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String custIdStr) {
        String[] custIds = custIdStr.split(",");
        List<String> customerIdsList = Arrays.asList(custIds);
        int a = customerService2.updateStatusBatch(customerIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除用户成功");
        } else {
            return ResultUtil.error("删除用户失败");
        }
    }

}
